# -*-coding:utf-8 -*-

"""
# File       : QuantityOfApiInMushup.py
# Time       ：2022/11/23 16:05
# Author     ：yang chen
# Dataset    : @article{liu2021data,
#   title={Data correction and evolution analysis of the ProgrammableWeb service ecosystem},
#   author={Liu, Mingyi and Tu, Zhiying and Zhu, Yeqi and Xu, Xiaofei and Wang, Zhongjie and Sheng, Quan Z},
#   journal={Journal of Systems and Software},
#   pages={111066},
#   year={2021},
#   publisher={Elsevier}
# }
"""

import json
from collections import Counter
from wordcloud import WordCloud
from itertools import *
import csv
import matplotlib.pyplot as plt

api_number_of_every_mushup = dict()  # 每一个mushup调用的api个数
number_of_every_api_used = dict()  # 每一个api被调用的次数
number_of_apis_provided_by_every_provider = dict()  # 每个提供商所提供的api个数
association_of_category_and_api = dict()  # mushup的category与api的关联情况
combined_two_categories = dict()  # mushup中两个category的组合需求
combined_three_categories = dict()  # mushup中三个category的组合需求
service_interface_protocol_of_api = dict()  # 每个api所使用的服务接口协议
association_of_service_protocol = dict()  # 表示实现某一种category所使用的api名称，次数以及接口服务类型


# 访问mushups_data文件
def mushups_data(filepath):
    with open(filepath) as f:
        contents = f.read()
        contents = json.loads(contents)  # 将读取到的文件中的内容转换为json类型
        for content in contents:
            if content:
                api_number_of_every_mushup[content['title']] = len(content['related_apis'])  # 记录每个mushup调用的api个数
                # 记录api被调用的次数
                for api in content['related_apis']:
                    if not api:
                        continue
                    if api['title'] in number_of_every_api_used:
                        number_of_every_api_used[api['title']] += 1
                    else:
                        number_of_every_api_used[api['title']] = 1
                categories = set(content['categories'])
                # 记录mushup中三个category需求的组合情况
                if len(categories) > 2:
                    combined_three_categories_list = list(combinations(categories, 3))
                    can_combine_three = True
                else:
                    can_combine_three = False
                if can_combine_three:
                    for combined_category in combined_three_categories_list:
                        combined_category = tuple(sorted(combined_category))
                        if combined_category in combined_three_categories:
                            combined_three_categories[combined_category] += 1
                        else:
                            combined_three_categories[combined_category] = 1
                # 记录mushup中两个个category需求的组合情况
                if len(categories) > 1:
                    combined_two_categories_list = list(combinations(categories, 2))
                    can_combine_two = True
                else:
                    can_combine_two = False
                if can_combine_two:
                    for combined_category in combined_two_categories_list:
                        combined_category = tuple(sorted(combined_category))
                        if combined_category in combined_two_categories:
                            combined_two_categories[combined_category] += 1
                        else:
                            combined_two_categories[combined_category] = 1
                # 记录mushup中category与api的组合情况
                for category in categories:
                    related_apis = content['related_apis']
                    for related_api in related_apis:
                        if related_api:
                            styles = []
                            for version in related_api['versions']:
                                styles.append(version['style'])
                            if related_api['title'] not in service_interface_protocol_of_api:
                                service_interface_protocol_of_api[related_api['title']] = styles
                            category_and_api = (category, related_api['title'])
                            if category_and_api in association_of_category_and_api:
                                association_of_category_and_api[category_and_api] += 1
                            else:
                                association_of_category_and_api[category_and_api] = 1


# 访问api_version_accessbiliby文件
def api_accessbiliby(filepath):
    with open(filepath) as f:
        contents = f.read()
        contents = json.loads(contents)
        for content in contents:
            if content:
                visit_statuss = content['visit_status']
                providers = set()
                for visit_status in visit_statuss:
                    # 从visit_url中提取提供商的url
                    url = visit_status['visit_url']
                    url_splits = url.split('/')
                    provider = url_splits[0] + '//' + url_splits[2]
                    providers.add(provider)
                    # 记录每个提供商提供的api个数
                    for provider in providers:
                        if provider in number_of_apis_provided_by_every_provider:
                            number_of_apis_provided_by_every_provider[provider] += 1
                        else:
                            number_of_apis_provided_by_every_provider[provider] = 1
                    providers.clear()


# 将整理好的数据写入csv文件
def writecsv(datas, filename):
    f = open('csvfile/' + filename + '.csv', mode='w', encoding='utf-8', newline='')
    csvwriter = csv.writer(f)
    for data in datas:
        csvwriter.writerow(data)
    f.close()


mushups_data('Correted-ProgrammableWeb-dataset-main/data/raw/api_mashup/active_mashups_data.txt')
mushups_data('Correted-ProgrammableWeb-dataset-main/data/raw/api_mashup/deadpool_mashups_data.txt')
api_accessbiliby(
    'Correted-ProgrammableWeb-dataset-main/data/raw/accessibility/api_accessibility/api_version_accessbiliby-1.txt')
api_accessbiliby(
    'Correted-ProgrammableWeb-dataset-main/data/raw/accessibility/api_accessibility/api_version_accessbiliby-2.txt')
api_accessbiliby(
    'Correted-ProgrammableWeb-dataset-main/data/raw/accessibility/api_accessibility/api_version_accessbiliby-3.txt')
api_accessbiliby(
    'Correted-ProgrammableWeb-dataset-main/data/raw/accessibility/api_accessibility/api_version_accessbiliby-4.txt')
api_accessbiliby(
    'Correted-ProgrammableWeb-dataset-main/data/raw/accessibility/api_accessibility/api_version_accessbiliby-5.txt')

# 按api个数对api_number_of_every_mushup排序
api_number_of_every_mushup_sorted = sorted(api_number_of_every_mushup.items(), key=lambda kv: (kv[1], kv[0]),
                                           reverse=True)
writecsv(api_number_of_every_mushup_sorted, 'api_number_of_every_mushup_sorted')  # 将排好序的数据写入csv文件
# 按api被使用的次数对number_of_every_api_used排序
number_of_every_api_used_sorted = sorted(number_of_every_api_used.items(), key=lambda kv: (kv[1], kv[0]), reverse=True)
writecsv(number_of_every_api_used_sorted, 'number_of_every_api_used_sorted')  # 将排好序的数据写入csv文件
# 将被使用次数最多的50个api制作成云图
number_of_every_api_used_top50 = {}
for i in range(50):
    number_of_every_api_used_top50[number_of_every_api_used_sorted[i][0][:-18]] = number_of_every_api_used_sorted[i][1]
wc = WordCloud(
    font_path='ttf/simhei.ttf',
    scale=4
)
wc.generate_from_frequencies(number_of_every_api_used_top50)
wc.to_file('image/number_of_every_api_used_top50.png')

# 按提供商提供的api个数对number_of_apis_provided_by_every_provider排序
number_of_apis_provided_by_every_provider_sorted = sorted(number_of_apis_provided_by_every_provider.items(),
                                                          key=lambda kv: (kv[1], kv[0]), reverse=True)
writecsv(number_of_apis_provided_by_every_provider_sorted,
         'number_of_apis_provided_by_every_provider_sorted')  # 将排好序的数据写入csv文件
# 将提供api最多的50个提供商制作成云图
number_of_apis_provided_by_every_provider_top50 = {}
for i in range(50):
    number_of_apis_provided_by_every_provider_top50[number_of_apis_provided_by_every_provider_sorted[i][0]] = \
    number_of_apis_provided_by_every_provider_sorted[i][1]
wc = WordCloud(
    font_path='ttf/simhei.ttf',
    scale=4
)
wc.generate_from_frequencies(number_of_apis_provided_by_every_provider_top50)
wc.to_file('image/number_of_apis_provided_by_every_provider_top50.png')

# 按category与api的组合次数对association_of_category_and_api排序
association_of_category_and_api_sorted = sorted(association_of_category_and_api.items(), key=lambda kv: (kv[1], kv[0]),
                                                reverse=True)
writecsv(association_of_category_and_api_sorted, 'association_of_category_and_api_sorted')  # 将排好序的数据写入csv文件

# 按category的组合次数对combined_two_categories排序
combined_two_categories_sorted = sorted(combined_two_categories.items(), key=lambda kv: (kv[1], kv[0]), reverse=True)
writecsv(combined_two_categories_sorted, 'combined_two_categories_sorted')  # 将排好序的数据写入csv文件
# 按category的组合次数对combined_two_categories排序
combined_three_categories_sorted = sorted(combined_three_categories.items(), key=lambda kv: (kv[1], kv[0]),
                                          reverse=True)
writecsv(combined_three_categories_sorted, 'combined_three_categories_sorted')  # 将排好序的数据写入csv文件
# 利用api_and_style与association_of_category_and_api_sorted整理出association_of_style
for x in association_of_category_and_api_sorted:
    if x[0][0] in association_of_service_protocol:
        association_of_service_protocol[x[0][0]].append([x[0][1], x[1], service_interface_protocol_of_api[x[0][1]]])
    else:
        association_of_service_protocol[x[0][0]] = [[x[0][1], x[1], service_interface_protocol_of_api[x[0][1]]]]
# 对每一种category找出其使用次数最多的50个（若不足50个则全部计算）api对应的服务借口协议
Auxiliary_Counter_array = []
for value in association_of_service_protocol.values():
    for i in range(50):
        try:
            Auxiliary_Counter_array.append(tuple(value[i][2]))
        except:
            break
# 统计各个服务借口协议的使用次数
counter = Counter(Auxiliary_Counter_array)
# 绘制饼图更直观地表示出各个接口协议所占比重
keys = list(counter.keys())  # 饼图的label
values = list(counter.values())  # 饼图的数值
pie_data = {}
# 部分接口协议使用次数少的将其合并为others
others_total = 0
# 使用次数最多的7个服务接口协议单独列出，其余的合并
for i in range(7):
    pie_data[keys[i]] = values[i]
for i in range(7, len(values)):
    others_total += values[i]
pie_data['others'] = others_total
# 绘制饼图
plt.rcParams['axes.facecolor'] = '#cc00ff'
plt.axis('equal')
patches, l_text, p_text = plt.pie(pie_data.values(), labels=pie_data.keys(), autopct='%.1f%%', shadow=True, radius=1,
                                  startangle=90)
plt.title('Service interface protocol usage frequency')
for t in p_text:
    t.set_size(6)
for t in l_text:
    t.set_size(9)
plt.savefig('image/frequency_of_service_interface.png')